package com.umeox.watch.moto.alarm.adapter;

import android.annotation.SuppressLint;
import android.util.LayoutDirection;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.text.TextUtilsCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.watch.themes.ItemClickListener;
import com.umeox.moto.watch.themes.LanguageBean;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;
import com.umeox.watch.moto.alarm.R;
import com.umeox.watch.moto.alarm.base.Constants;

import java.util.List;
import java.util.Locale;


public class WeekChooseAdapter extends RecyclerView.Adapter<WeekChooseAdapter.ItemViewHolder> {

    private List<LanguageBean> list;

    private ItemClickListener itemClickListener;
    private int mSelectedPosition = -1;


    public WeekChooseAdapter(List<LanguageBean> list, ItemClickListener itemClickListener) {
        this.list = list;
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_week_choose, parent, false);

        return new ItemViewHolder(itemView);
    }

    public Locale getSelectedLanguage() {
        return list.get(mSelectedPosition).getLocale();
    }

    StringBuffer expressionBuffer = new StringBuffer();
    public String getExpression() {
        if (list.get(0).isSelection()) return Constants.ALARM_EVERYDAY_REPEAT_EXP;
        expressionBuffer.delete(0, expressionBuffer.length());
        for (int i = 1; i < list.size(); i++) {
            expressionBuffer.append(list.get(i).isSelection() ? 1 : 0);
        }
        return expressionBuffer.toString();
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {

        final LanguageBean languageBean = list.get(position);

        holder.mItemTv.setText(String.format(Locale.getDefault(),"%s",languageBean.getLanguage()));
        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
            holder.mItemTv.setCompoundDrawablesRelativeWithIntrinsicBounds(ResourceManager.getInstance().getDrawable(R.drawable.check_box_selector), null, null, null);
        } else {
            holder.mItemTv.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, ResourceManager.getInstance().getDrawable(R.drawable.check_box_selector), null);
        }



        final boolean selection = languageBean.isSelection();

        holder.mItemTv.setSelected(selection);
        holder.itemView.setOnClickListener(v -> {

            if (position == 0) {
                for (LanguageBean bean : list) {
                    bean.setSelection(false);
                }
                languageBean.setSelection(!selection);
                notifyDataSetChanged();
                return;

            }
            list.get(0).setSelection(false);
            list.get(position).setSelection(!selection);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView mItemTv;


        public ItemViewHolder(View itemView) {
            super(itemView);
            mItemTv = itemView.findViewById(R.id.rb_language);

        }
    }
}

